package sz.szsmk.citizencard.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.text.NumberFormat;
import java.util.regex.Pattern;

/**
 * 字符串操作工具包
 *
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class StringCommonUtil {

    private final static String TAG = StringCommonUtil.class.getSimpleName();


    private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

    private final static Pattern mobilePattern = Pattern.compile("^(13[0-9]|15[0-9]|18[0-9]|14[57]|17[0-9])[0-9]{8}$");


    /**
     * 判断给定字符串是否空白串。
     * 空白串是指由空格、制表符、回车符、换行符组成的字符串
     * 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是不是一个合法的手机号码
     */
    public static boolean isMobile(Context context, String mobile) {
        if (!TextUtils.isEmpty(mobile) && mobile.length() == 11) {
            return true;
        } else {
            return false;
        }

//		if(mobile == null || mobile.length() == 0){
//			return false;
//		}
//		String mobileVlai = SharedPreferenceSetting.getMobileVali(context);
//		if(mobileVlai != null && !"".equals(mobileVlai)){
//			Pattern mobilePattern = Pattern.compile(mobileVlai);
//			Logger.e("isMobile", "============" + mobileVlai);
//			return mobilePattern.matcher(mobile).matches();
//		}
//
//		return mobilePattern.matcher(mobile).matches();
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if (email == null || email.trim().length() == 0)
            return false;
        return emailer.matcher(email).matches();
    }

    /**
     * 验证身份证
     **/
    public static boolean isIdCard(String cardNo) {
        if (cardNo == null || cardNo.trim().length() == 0) {
            return false;
        }
        if (cardNo.length() == 15) {
            Pattern cardNoMatch = Pattern.compile("(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)");
            return cardNoMatch.matcher(cardNo).matches();
        } else if (cardNo.length() == 18) {
            //^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$
            Pattern cardNoMatch = Pattern.compile("(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|[x|X])$)");
            return cardNoMatch.matcher(cardNo).matches();
        }
        return false;
    }

    /**
     *
     * */
    public static String getCategory(String type) {
        String category = "7";
        if ("全部".equals(type)) {
            category = "7";
        } else if ("自由".equals(type)) {
            category = "5";
        } else if ("加盟".equals(type)) {
            category = "4";
        } else if ("自助终端".equals(type)) {
            category = "3";
        }
        return category;
    }

    /**
     * 判断字符串是否为空
     **/
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * toast显示信息
     **/
    private static Toast mToast = null;

    public static void showMsg(Context context, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg,
                    Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    public static String getRankingPercent(double values) {
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(3);        // 保留到小数点后几位        显示：47.00%
        return nf.format(values);
    }

    public static String changeToStarString(String s) {
        if (StringUtils.isEmpty(s)) {
            return "";
        }
        if (s.length() <= 8) {
            return s;
        }
        if (s.length() > 8) {
            StringBuilder newString = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                if (i < 4 || i < s.length() && i > s.length() - 5) {
                    newString.append(s.charAt(i) + ((i == 3) ? " " : ""));
                } else {
                    if (i % 4 == 3||i == (s.length() - 5)) {
                        newString.append("* ");
                    } else {
                        newString.append("*");
                    }
                }
            }
            Log.d("clark", newString.toString());
            return newString.toString();
        }
        return "";
    }
}
